Control Flow

Compound Expressions


In [57]:
workspace()
z = begin
    x = 1
    y = 2
    x + y
end

println(z)

#using the (;) chain syntax
y = begin x = 1; y = 2; x + y end
println(y)


3
3

Conditional Expressions


In [58]:
a = 3
b = 4

if a < b
println("a is less than b")
elseif a > b
println("a is greater than b")
else
println("a is equal to b")
end


a is less than b

Short Circuit Evaluation


In [59]:
workspace()
t(x) = (println(x); true)
f(x) = (println(x); false)

t(4) || f(1)
# As the first function returns a true value, the statment is true, so it stops evaluating


4
Out[59]:
true

Repeared Evaluation: Loops

While loop


In [60]:
i = 1
while i <= 5
println(i)
i += 1
end


1
2
3
4
5

In [61]:
j=1
while true
         println(j)
         if j >= 5
           break
         end
         j += 1
       end


1
2
3
4
5

For loop


In [62]:
for i = 1:5
println(i)
end


1
2
3
4
5

In [63]:
#you can use for to iterate over arrays
for i in [1,4,0]
         println(i)
end
str1 ="Hello"
for j in str1
    println(j)
end


1
4
0
H
e
l
l
o

Try Catch


In [64]:
workspace()
k(x) = try
sqrt(x)
catch
sqrt(complex(x, 0))
end

println(k(4))
k(-4)


2.0
Out[64]:
0.0 + 2.0im

In [ ]:


In [ ]: